package demo.jdk.features.jdk5;

import java.util.Arrays;

/**
 * @author Ranchoz  2018/9/9 10:54
 **/
public class BubbleSortThread extends Thread {


	public static void main(String[] args) {
		int numbers[] = new int[] {12,32,53,71,9};
		BubbleSortThread t = new BubbleSortThread(numbers);
		t.start();
	}

	private int[] numbers;

	public BubbleSortThread(int[] numbers) {
		setName("Simple Thread");
		setUncaughtExceptionHandler(
				new SimpleThreadExceptionHandler());
		this.numbers = numbers;
	}

	@Override
	public void run() {
		int index = numbers.length;
		boolean finished = false;
		while (!finished) {
			index--;
			finished = true;
			for (int i = 0; i < index; i++) {
				// Create error condition
				if (numbers[i + 1] < 0) {
					throw new IllegalArgumentException(
							"Cannot pass negative numbers into this thread!");
				}
				if (numbers[i] > numbers[i + 1]) {
					// swap
					int temp = numbers[i];
					numbers[i] = numbers[i + 1];
					numbers[i + 1] = temp;
					finished = false;
				}
			}
		}
		Arrays.stream(numbers).forEach(i -> System.out.println(i));
	}
}

class SimpleThreadExceptionHandler implements
		Thread.UncaughtExceptionHandler {
	public void uncaughtException(Thread t, Throwable e) {
		System.err.printf("%s: %s at line %d of %s%n",
				t.getName(),
				e.toString(),
				e.getStackTrace()[0].getLineNumber(),
				e.getStackTrace()[0].getFileName());
	}
}

